using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using OpenComposite.Base.Collections;
using OpenComposite.Base.Designer;
using OpenComposite.EII.Repository;
using OpenComposite.Mapper;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.Designer.SCA
{
	public class CapabilitySymbol : DesignerControl, IConnector, IConnectorFactory, IRepositoryItemSymbol
	{
		#region Initialization
		public CapabilitySymbol()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();
			_operation = null;
			_logicalActivity = null;
			_lstRoutes = new List<Route>();

			// this
			this.Size = new Size(20, 20);

			// _penBorder
			_penBorder = new Pen(Color.Blue);

			// _brushBack
			setBackgroundBrush();

			// _textbox
			_textbox = new NoClipTextBox();
			_textbox.AutoSize = false;
			_textbox.ReadOnly = true;
			_textbox.Bounds = new Rectangle(-50, Height, 120, 40);
			_textbox.TextAlign = HorizontalAlignment.Center;
			//_textbox.Text = this.Text;
			//_textbox.TextChanged += new EventHandler(_textbox_TextChanged);

			this.ContextMenuStrip.Items.Clear();
			//this.ContextMenuStrip.Items.Add("Subscription Mapping...", null,
			//    new EventHandler(SubscriptionMapping_Click));

			this.Controls.Add(_textbox);

			this.EndUpdate();
		}

		#endregion

		#region Public Members

		#region Properties
		// new
		public Color BorderColor
		{
			get { return _penBorder.Color; }
			set
			{
				if ( _penBorder.Color != value ) {
					_penBorder.Color = value;
					this.Update();
				}
			}
		}

		[TypeConverter(typeof(ExpandableObjectConverter))]
		public Repository.Operation Operation
		{
			get
			{
				if ( _operation == null && ( _logicalActivity == null || _logicalActivity.OperationID < 1 ) ) return null;
				Operation op = Global.Data.RepositoryService.GetItem<Operation>(_logicalActivity.OperationID);
				return _operation == null ? op : _operation;
			}
			set
			{
				if ( _operation != value ) {
					Repository.Operation oldOp = _operation;
					_operation = value;
					OnOperationChanged(oldOp, _operation);
				}
			}
		}

		// override
		public override bool CanFocused
		{
			get
			{
				return true;
			}
		}
		public override bool CanMove
		{
			get
			{
				return true;
			}
		}
		public override bool CanResize
		{
			get
			{
				return false;
			}
		}
		public override bool CanSelect
		{
			get
			{
				return true;
			}
		}
		public override bool UseGrid
		{
			get
			{
				return true;
			}
		}

		public override string Text
		{
			get
			{
				string s = _textbox.Text;
				if ( _logicalActivity != null ) {
					Verb v = this._logicalActivity.Verb;
					if ( v != null ) {
						s = v.Name;
						_textbox.Text = s;
					}
				}
				return s;
			}
			set
			{
				_textbox.Text = value;
			}
		}
		#endregion

		#region Events
		public event EventHandler OperationChanged = null;
		#endregion

		#region Methods
		// override
		public override void Paint(System.Windows.Forms.PaintEventArgs pe)
		{
			if ( !this.Visible )
				return;

			Rectangle rpaint = this.GetPaintBounds();

			// paint circle
			pe.Graphics.FillEllipse(this._brushBack, rpaint);
			pe.Graphics.DrawEllipse(_penBorder, rpaint);

			base.Paint(pe);
		}
		public override void Dispose()
		{
			dispose(_penBorder);
			dispose(_textbox);
			base.Dispose();
		}
		public override object GetDragObject(Point location, System.Windows.Forms.MouseButtons button)
		{
			return this;
		}
		public override void DropObject(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			if ( button == System.Windows.Forms.MouseButtons.Right ) {
				if ( obj is OrchestrationComponentSymbol || obj is ReferenceSymbol ) {
					this.DesignerViewControl.AddRoute((DesignerControl)obj, this);
				} else if ( obj is InterfaceSymbol ) {
					FormSelectOperations frm = new FormSelectOperations(_operation);
					frm.MultiSelect = false;
					if ( frm.ShowDialog(( (InterfaceSymbol)obj ).Operations) == DialogResult.OK &&
						 frm.SelectedOperations.Length > 0 ) {
						this.Operation = frm.SelectedOperations[0];
						this.DesignerViewControl.AddRoute(this, (DesignerControl)obj);
					}
				} else if ( obj is ExposedCapabilitySymbol ) {
					RepositorySurfaceManager manager = _logicalActivity.GetService(typeof(RepositorySurfaceManager)) as RepositorySurfaceManager;
					if ( manager == null ) return;
					RepositorySurface surface = manager.ActiveDesignSurface as RepositorySurface;
					if ( surface == null || surface.Controller == null || surface.Controller.RepositoryItem == null ) return;
					WorkflowReferenceSymbol wfRef = ( (DesignerControl)obj ).Parent as WorkflowReferenceSymbol;
					if ( wfRef == null ) return;
					this.DesignerViewControl.AddRoute(this, (DesignerControl)obj);
					if ( _operation == null ) {
						IRepositoryItem parent = null;
						_operation = Global.Data.RepositoryService.GetNewItem<Operation>(false, ref parent);
						_operation.Name = _logicalActivity.Name;
					}
					_operation.WorkflowId = surface.Controller.RepositoryItem.ID;
					_operation.ExposedVerbID = _logicalActivity.VerbID;
					_operation.ExposedWorkflowId = wfRef.RepositoryItem.ID;
					_logicalActivity.OperationID = _operation.ID;
					_logicalActivity.Save();
					_operation.Save();
					//_operation.ExposedWorkflowId =
				}
			}
		}
		public override bool IsDropAllowed(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			return this.IsDropAllowed(obj.GetType(), location, button);
		}
		public override bool IsDropAllowed(Type type, Point location, System.Windows.Forms.MouseButtons button)
		{
			if ( button == System.Windows.Forms.MouseButtons.Right ) {
				return ( type == typeof(OrchestrationComponentSymbol) ||
						 type == typeof(InterfaceSymbol) ||
						 type == typeof(ReferenceSymbol) ||
						 type == typeof(ExposedCapabilitySymbol) );
			} else {
				return false;
			}
		}

		public override void ReadXml(System.Xml.XmlReader reader)
		{
			RepositoryService repSvc = (RepositoryService)this.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			System.ComponentModel.BooleanConverter bc = new System.ComponentModel.BooleanConverter();
			try {
				while ( reader.Read() ) {
					switch ( reader.Name ) {
						case "DesignerControl":
							if ( reader.NodeType == System.Xml.XmlNodeType.EndElement )
								return;
							break;
						case "LogicalActivity":
							if ( reader.MoveToAttribute("id") && reader.ReadAttributeValue() ) {
								_logicalActivity = repSvc.GetItem(ItemType.LogicalActivity, int.Parse(reader.Value), false) as LogicalActivity;
								if ( _logicalActivity != null )
									this.Text = _logicalActivity.Name;
							}
							if ( reader.MoveToAttribute("isMainControl") && reader.ReadAttributeValue() )
								if ( reader.Value == true.ToString() )
									this.DesignerViewControl.MainControl = this;
							if ( reader.MoveToAttribute("isRef") && reader.ReadAttributeValue() )
								this.IsReference = (bool)bc.ConvertFromString(reader.Value);
							break;
						case "Operation":
							if ( reader.MoveToAttribute("id") && reader.ReadAttributeValue() ) {
								_operation = repSvc.GetItem(ItemType.Operation, int.Parse(reader.Value), false) as Operation;
							}
							break;
						case "DesignerControls":
							// read child controls
							ReadChildControls(reader);
							break;
						default:
							ReadStandardProperties(reader);
							break;
					}
				}
			} finally {

			}
		}
		public override void WriteXml(System.Xml.XmlWriter writer)
		{
			System.ComponentModel.BooleanConverter bc = new System.ComponentModel.BooleanConverter();
			WriteStandardProperties(writer);

			if ( _logicalActivity != null ) {
				writer.WriteStartElement("LogicalActivity");
				writer.WriteAttributeString("id", _logicalActivity.ID.ToString());
				writer.WriteAttributeString("name", _logicalActivity.Name);
				if ( this.DesignerViewControl.MainControl == this )
					writer.WriteAttributeString("isMainControl", true.ToString());
				else
					writer.WriteAttributeString("isMainControl", false.ToString());
				writer.WriteAttributeString("isRef", bc.ConvertToString(this.IsReference));
				writer.WriteEndElement();
			}
			if ( _operation != null ) {
				writer.WriteStartElement("Operation");
				writer.WriteAttributeString("id", _operation.ID.ToString());
				writer.WriteAttributeString("name", _operation.Name);
				writer.WriteEndElement();
			}

			WriteChildControls(writer);
		}

		protected override void ReadStandardProperties(XmlReader reader)
		{
			base.ReadStandardProperties(reader);
			switch ( reader.Name ) {
				case "TextboxBounds":
					RectangleConverter rc = new RectangleConverter();
					string s = reader.ReadString();
					this._textbox.Bounds = (Rectangle)rc.ConvertFromString(null, System.Globalization.CultureInfo.InvariantCulture, s.Replace(';', ','));
					break;
			}
		}

		protected override void WriteChildControls(XmlWriter writer)
		{
			RectangleConverter rc = new RectangleConverter();
			base.WriteChildControls(writer);
			writer.WriteElementString("TextboxBounds", rc.ConvertToString(_textbox.Bounds));
		}
		//protected override void WriteChildControls(XmlWriter writer)
		//{
		//    bool controlsStartAdded = false;
		//    if ( this.Controls.Count > 0 ) {
		//        foreach ( DesignerControl dc in this.Controls ) {
		//            if ( dc is OpenComposite.Base.Designer.ListView || dc is OpenComposite.Base.Designer.Button || dc is OpenComposite.Base.Designer.ComboBox )
		//                continue;
		//            if ( !controlsStartAdded ) {
		//                writer.WriteStartElement("DesignerControls");
		//                controlsStartAdded = true;
		//            }
		//            writer.WriteStartElement("DesignerControl");
		//            dc.WriteXml(writer);
		//            writer.WriteEndElement();
		//        }
		//        if ( controlsStartAdded ) {
		//            writer.WriteEndElement();
		//        }
		//    }
		//}
		//protected override void ReadChildControls(XmlReader reader)
		//{
		//    if ( reader.IsEmptyElement || reader.NodeType == XmlNodeType.EndElement )
		//        return;

		//    while ( reader.Read() ) {
		//        switch ( reader.Name ) {
		//            case "DesignerControls":
		//                if ( reader.NodeType == XmlNodeType.EndElement )
		//                    return;
		//                break;
		//            case "DesignerControl":
		//                if ( reader.NodeType == XmlNodeType.Element ) {
		//                    string typename = reader.GetAttribute("Type");
		//                    if ( typename == "OpenComposite.Base.Designer.NoClipTextBox" ) {
		//                        _textbox = (NoClipTextBox)AddSerializedDesignerControl(typename, reader, false);
		//                        _textbox.AutoSize = false;
		//                        _textbox.ReadOnly = true;
		//                        _textbox.TextAlign = HorizontalAlignment.Center;
		//                    }
		//                }
		//                break;
		//        }
		//    }
		//}

		public override object GetService(Type serviceType)
		{
			if ( _operation != null )
				return _operation.GetService(serviceType);
			else
				return base.GetService(serviceType);
		}
		#endregion

		#endregion

		#region Protected Members

		#region Methods
		// new
		protected void CalculateRouteDockDirection(Rectangle rStart, Rectangle rEnd,
			out RouteDockDirection dockStart, out RouteDockDirection dockEnd)
		{
			dockStart = RouteDockDirection.Right;
			dockEnd = RouteDockDirection.Left;
			try {
				int x1 = rStart.X, y1 = rStart.Y, h1 = rStart.Height, w1 = rStart.Width;
				int x2 = rEnd.X, y2 = rEnd.Y, h2 = rEnd.Height, w2 = rEnd.Width;
				int dx = 30, dy = 30;

				// t1    |     | dx |
				// ------|-----|----|------
				// t2    |    _|_   |  dy
				// ------|---|_1_|h1|------     
				// t3    |     w1   |
				// ------|-----|----|------
				// t4    |     |    |
				//    l1 | l2  | l3 | l4
				bool t1 = ( y2 + h2 + dy < y1 );
				bool t4 = ( y2 > y1 + h1 + dy );
				bool t2 = !t1 && ( y2 + h2 < y1 + h1 / 2 );
				bool t3 = !t4 && ( y2 > y1 + h1 / 2 );
				bool l1 = ( x2 + w2 + dx < x1 );
				bool l4 = ( x2 > x1 + w1 + dx );
				bool l2 = !l1 && ( x2 + w2 < x1 + w1 / 2 );
				bool l3 = !l4 && ( x2 > x1 + w1 / 2 );

				if ( l1 ) {
					dockStart = RouteDockDirection.Left;
					dockEnd = RouteDockDirection.Right;
				} else if ( l4 ) {
					dockStart = RouteDockDirection.Right;
					dockEnd = RouteDockDirection.Left;
				} else if ( t1 ) {
					dockStart = RouteDockDirection.Top;
					dockEnd = RouteDockDirection.Bottom;
				} else if ( t4 ) {
					dockStart = RouteDockDirection.Bottom;
					dockEnd = RouteDockDirection.Top;
				} else if ( t2 && l2 ) {
					dockStart = RouteDockDirection.Left;
					dockEnd = RouteDockDirection.Bottom;
				} else if ( t3 && l2 ) {
					dockStart = RouteDockDirection.Left;
					dockEnd = RouteDockDirection.Top;
				} else if ( t3 && l3 ) {
					dockStart = RouteDockDirection.Right;
					dockEnd = RouteDockDirection.Top;
				} else if ( t2 && l3 ) {
					dockStart = RouteDockDirection.Right;
					dockEnd = RouteDockDirection.Bottom;
				}
			} catch { }
		}

		protected virtual void OnOperationChanged(Repository.Operation oldOp, Repository.Operation newOp)
		{
			if ( _logicalActivity == null )
				return;

			DesignerControl[] dctrls = this.GetPredecessors();
			foreach ( DesignerControl dc in dctrls ) {
				ReferenceSymbol rs = dc as ReferenceSymbol;
				if ( rs != null ) {
					if ( oldOp != null )
						rs.Operations.Remove(oldOp);
					if ( newOp != null )
						rs.Operations.Add(newOp);
				}
			}
			if ( newOp != null )
				this._logicalActivity.OperationID = newOp.ID;
			else
				this._logicalActivity.OperationID = -1;

			EventHandler tmp = this.OperationChanged;
			if ( tmp != null )
				tmp(this, EventArgs.Empty);
		}

		// override
		protected override void OnLocationChanged(EventArgs e)
		{
			setBackgroundBrush();
			base.OnLocationChanged(e);

			if ( this.Parent != null ) {
				int bottom = ( this.Location.Y + this.Height + this._textbox.Height );
				if ( this.Parent.Height < bottom ) {
					this.Parent.Height += bottom - this.Parent.Height;
					//this.Parent.Size = new Size(this.Parent.Width, this.Parent.Height + bottom - this.Parent.Height);
				}
			}
		}
		protected override void OnSizeChanged(EventArgs e)
		{
			setBackgroundBrush();
			base.OnSizeChanged(e);
		}
		protected override void OnParentChanged(EventArgs e)
		{
			setBackgroundBrush();
			base.OnParentChanged(e);
		}
		protected override void OnParentLocationChanged()
		{
			setBackgroundBrush();
			base.OnParentLocationChanged();
		}
		protected override void OnParentSizeChanged()
		{
			setBackgroundBrush();
			base.OnParentSizeChanged();
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private NoClipTextBox _textbox;
		private Pen _penBorder;
		private LinearGradientBrush _brushBack;

		private Repository.Operation _operation;

		private List<Route> _lstRoutes;
		#endregion

		#region Methods
		private Point getDefaultDockPoint(RouteDockDirection dock)
		{
			Rectangle bounds = this.GetPaintBounds();
			Point p = new Point();
			switch ( dock ) {
				case RouteDockDirection.Top:
					p.X = bounds.Left + ( ( bounds.Right - bounds.Left ) / 2 );
					p.Y = bounds.Top;
					break;
				case RouteDockDirection.Bottom:
					p.X = bounds.Left + ( ( bounds.Right - bounds.Left ) / 2 );
					p.Y = bounds.Bottom;
					break;
				case RouteDockDirection.Right:
					p.X = bounds.Right;
					p.Y = bounds.Top + ( ( bounds.Bottom - bounds.Top ) / 2 );
					break;
				case RouteDockDirection.Left:
					p.X = bounds.Left;
					p.Y = bounds.Top + ( ( bounds.Bottom - bounds.Top ) / 2 );
					break;
			}
			return p;
		}
		private void setBackgroundBrush()
		{
			dispose(_brushBack);
			_brushBack = new LinearGradientBrush(this.GetPaintBounds(),
				Color.YellowGreen, Color.White, LinearGradientMode.ForwardDiagonal);
		}
		private void setInputMappingPhysical()
		{
			NamedBusinessObjectCollection lstSubscriptions = _logicalActivity.GetSubscribedBusinessObjects();
			if ( lstSubscriptions.Count == 0 )
				return;

			List<XmlSchema> schemas = new List<XmlSchema>();
			foreach ( Repository.BusinessObject bo in lstSubscriptions.BusinessObjects ) {
				schemas.Add(bo.Schema);
			}

			XmlSchema xsdSubscriptions = createCompoundDocument(schemas);

			FormMapping frm = new FormMapping();
			BindingListEx<Repository.WebMethod> lstWebMethods = Global.Data.GetOperationWebMethods(_logicalActivity.OperationID);
			if ( lstWebMethods.Count == 0 ) {
				MessageBox.Show("No Binding to WebMethod exists.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}
			frm.InitializeMapper(xsdSubscriptions, lstWebMethods[lstWebMethods.Count - 1].InputSchema,
				_logicalActivity.PhysicalInputXslt, _logicalActivity.PhysicalInputConnections, _logicalActivity.ExtendedProperties.InputCodeMapping);
			frm.UseCodeMapping = _logicalActivity.ExtendedProperties.UseCodeMapping;
			if ( frm.ShowDialog() == DialogResult.OK ) {
				string map;
				XsltDocument xslt = frm.GetMapping(out map);
				if ( xslt != null && xslt.DocumentElement != null ) {
					_logicalActivity.PhysicalInputConnections = map;
					_logicalActivity.PhysicalInputXslt = xslt.DocumentElement.OuterXml;
					_logicalActivity.Save();
				}
			}
		}
		private XmlSchema createCompoundDocument(List<XmlSchema> schemas)
		{
			if ( schemas.Count == 0 ) {
				return null;
			}

			XmlSchemaSet xset = new XmlSchemaSet();

			//init Compound Schema
			XmlSchema compSchema = new XmlSchema();
			compSchema.ElementFormDefault = XmlSchemaForm.Qualified;
			compSchema.AttributeFormDefault = XmlSchemaForm.Qualified;
			compSchema.TargetNamespace = schemas[0].TargetNamespace;
			compSchema.Namespaces.Add("tns", "http://www.theopencomposite.com/canonical_document");
			compSchema.Namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");

			//init choice-Element for adding Elements to the Schema
			XmlSchemaElement element = new XmlSchemaElement();
			element.Name = "SubscribedDocuments";
			XmlSchemaComplexType complexType = new XmlSchemaComplexType();
			element.SchemaType = complexType;
			XmlSchemaSequence seq = new XmlSchemaSequence();
			complexType.Particle = seq;

			int counter = 0;
			foreach ( XmlSchema schema in schemas ) {
				foreach ( XmlQualifiedName qname in schema.Namespaces.ToArray() ) {
					if ( qname.Namespace == "http://www.w3.org/2001/XMLSchema" ||
						 qname.Namespace == "http://www.theopencomposite.com/canonical_document" )
						continue;

					compSchema.Namespaces.Add(string.Format("compSch{0}", counter),
						qname.Namespace);
					counter++;
				}
				xset.Add(schema);
			}
			xset.Compile();
			if ( !xset.IsCompiled ) {
				throw new Exception("Compiling Schemas failed in FormLogicalActivityConfiguration.createCompoundDocument(..).");
			}
			for ( int i = 0; i < schemas.Count; i++ ) {
				foreach ( XmlSchemaObject schemaObj in schemas[i].Items ) {
					if ( schemaObj is XmlSchemaElement ) {
						bool exists = false;
						foreach ( XmlSchemaObject xmlObj in seq.Items ) {
							if ( xmlObj is XmlSchemaElement ) {
								if ( ( (XmlSchemaElement)xmlObj ).QualifiedName ==
									( (XmlSchemaElement)schemaObj ).QualifiedName ) {
									exists = true;
								}
							}
						}
						if ( exists == false ) {
							seq.Items.Add(schemaObj);
						}
					} else {
						if ( compSchema.Items.Contains(schemaObj) == false ) {
							compSchema.Items.Add(schemaObj);
						}
					}
				}
			}

			//add compound Element that contains the subscribed Elements
			if ( compSchema.Items.Contains(element) == false ) {
				compSchema.Items.Add(element);
			}

			xset = new XmlSchemaSet();
			xset.Add(compSchema);
			//xset.ValidationEventHandler += new ValidationEventHandler(this.validationEventHandler);
			xset.Compile();

			return compSchema;
		}

		#endregion

		#region Event Handlers
		private void _textbox_TextChanged(object sender, EventArgs e)
		{
			base.Text = _textbox.Text;
		}

		private void route_Removing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Route r = sender as Route;
			if ( r == null )
				return;
			if ( r.StartControl is InterfaceSymbol || r.EndControl is InterfaceSymbol ) {
				this.Operation = null;
			}
		}

		private void SubscriptionMapping_Click(object sender, EventArgs e)
		{
			setInputMappingPhysical();
		}
		#endregion

		#endregion

		#region IConnectorFactory Members
		public virtual bool CreateRouteStart(DesignerControl controlEnd,
			out IConnector connector, out LineCap cap, out CustomLineCap customcap,
			out RouteDockDirection dockStart, out RouteDockDirection dockEnd,
			out bool useStartConnectorAsRouteStart)
		{
			Rectangle rS = this.GetPaintBounds();
			Rectangle rE = controlEnd.GetPaintBounds();
			CalculateRouteDockDirection(rS, rE, out dockStart, out dockEnd);
			connector = this;
			cap = LineCap.NoAnchor;
			customcap = null;
			useStartConnectorAsRouteStart = false;
			return true;
		}
		public virtual bool CreateRouteEnd(DesignerControl controlStart,
			out IConnector connector, out LineCap cap, out CustomLineCap customcap,
			out RouteDockDirection dockStart, out RouteDockDirection dockEnd,
			out bool useEndConnectorAsRouteEnd)
		{
			Rectangle rS = controlStart.GetPaintBounds();
			Rectangle rE = this.GetPaintBounds();
			CalculateRouteDockDirection(rS, rE, out dockStart, out dockEnd);
			connector = this;
			cap = LineCap.NoAnchor;
			customcap = null;
			useEndConnectorAsRouteEnd = false;
			return true;
		}
		#endregion
		#region IConnector Members

		public virtual Point GetRouteStart(RouteDockDirection dock, Route route)
		{
			if ( !_lstRoutes.Contains(route) ) {
				_lstRoutes.Add(route);
				route.Removing += new System.ComponentModel.CancelEventHandler(route_Removing);
			}
			return getDefaultDockPoint(dock);
		}

		public virtual Point GetRouteEnd(RouteDockDirection dock, Route route)
		{
			if ( !_lstRoutes.Contains(route) ) {
				_lstRoutes.Add(route);
				route.Removing += new System.ComponentModel.CancelEventHandler(route_Removing);
			}
			return getDefaultDockPoint(dock);
		}

		#endregion

		#region IRepositoryItemSymbol Members

		public bool SaveHierarchy
		{
			get { return true; }
		}

		private bool _isReference = false;
		public bool IsReference
		{
			get { return _isReference; }
			set { _isReference = value; }
		}

		public bool CanBeMainControl
		{
			get { return false; }
		}

		private Repository.LogicalActivity _logicalActivity;
		public OpenComposite.EII.Repository.IRepositoryItem RepositoryItem
		{
			get
			{
				return _logicalActivity;
			}
			set
			{
				Debug.Assert(( value is Repository.LogicalActivity || value == null ), "Wrong Type");
				_logicalActivity = value as Repository.LogicalActivity;
				if ( _logicalActivity != null ) {
					if ( _logicalActivity.ID > 0 && _logicalActivity.Name != Resources.sNewLogicalActivity )
						this.Text = _logicalActivity.Name;
					else
						_logicalActivity.Name = this.Text;
				}
			}
		}

		public void Load(int idRepository, int idDomain)
		{

		}

		public bool Save(int idRepository, int idDomain)
		{
			if ( _logicalActivity == null )
				return false;

			return _logicalActivity.Save();
		}

		public bool Delete(bool bAsk)
		{
			if ( _logicalActivity == null )
				return true;

			return _logicalActivity.Delete(bAsk);
		}

		#endregion
	}
}
